package com.abbas.smartCity.ui.kt

import android.content.Context
import android.view.View
import android.widget.AdapterView
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.viewpager.widget.ViewPager
import com.abbas.smartCity.R
import com.abbas.smartCity.adapter.BannerImageAdapter2
import com.abbas.smartCity.adapter.KListCommAdapter
import com.abbas.smartCity.adapter.ListCommAdapter
import com.abbas.smartCity.base.BaseBindingModelFragment
import com.abbas.smartCity.bean.HomeBannerBean
import com.abbas.smartCity.bean.HomeNewsBean
import com.abbas.smartCity.bean.HomeNewsTypeBean
import com.abbas.smartCity.bean.ServerBean
import com.abbas.smartCity.databinding.FragmentHomeBinding
import com.abbas.smartCity.ui.DetailActivity
import com.abbas.smartCity.ui.HomeActivity
import com.abbas.smartCity.util.retrofit
import com.abbas.smartCity.util.run
import com.abbas.smartCity.util.startActivity
import com.google.android.material.tabs.TabLayout
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnBannerListener
import com.youth.banner.listener.OnPageChangeListener

class KHomeFragmentViewModel : ViewModel() {
    var homeBannerData = MutableLiveData<MutableList<HomeBannerBean.RowsBean>>()
    var serverData = MutableLiveData<MutableList<ServerBean.RowsBean>>()
    var newsTypeData = MutableLiveData<MutableList<HomeNewsTypeBean.DataBean>>()
    var newsData = MutableLiveData<MutableList<HomeNewsBean.RowsBean>>()
}

class KHomeFragment : BaseBindingModelFragment<FragmentHomeBinding, KHomeFragmentViewModel>() {


    override fun initData() {
        viewModel.homeBannerData.observe(this) { data ->
            binding.banner.apply {
                adapter = BannerImageAdapter2(data.map { it.advImg }.toMutableList())
                indicator = CircleIndicator(this@KHomeFragment.requireContext())
                start()
            }
        }
        viewModel.serverData.observe(this){
            binding.gv.apply {
                if (adapter != null){
                    (adapter as KListCommAdapter<*,*>).notifyDataSetChanged()
                }else {
                    adapter = KListCommAdapter(
                        it,
                        KListCommAdapter.ServerHolder::class.java,
                        R.layout.server_item
                    )
                }
            }
        }
        viewModel.newsTypeData.observe(this){
            binding.tab.apply {
                it.forEach {
                    newTab().apply {
                        text = it.name
                        tag = it.id
                    }.also { addTab(it) }
                }
            }
        }
        viewModel.newsData.observe(this){
            binding.lv.apply {
                if (adapter != null){
                    (adapter as KListCommAdapter<HomeNewsBean.RowsBean,*>).resetData(it)
                }else{
                    adapter = KListCommAdapter(it,KListCommAdapter.NewsHolder::class.java,R.layout.news_item)
                }
            }
        }
        retrofit {
            homeBanner.run {
                onSuccess {
                    viewModel.homeBannerData.postValue(it.rows)
                }
            }
            server.run {
                onSuccess{
                    it.rows.subList(0,9).apply {
                        add(ServerBean.RowsBean().apply {
                            serviceName = "更多服务"
                        })
                    }.also { data ->
                        viewModel.serverData.postValue(data)
                    }
                }
            }
            newsType.run {
                onSuccess{
                    viewModel.newsTypeData.postValue(it.data)
                }
            }
            getNews(9)
        }
    }

    override fun initEvent() {
        binding.banner.setOnBannerListener { _, p1 ->
            startActivity(DetailActivity::class.java){
                putExtra("id",viewModel.homeBannerData.value?.get(p1)?.targetId)
            }
        }
        binding.gv.setOnItemClickListener { adapterView, view, i, l ->
            when (i){
                9 -> HomeActivity.bnv.selectedItemId = R.id.all_server
            }
        }
        binding.tab.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                getNews(tab?.tag as Int)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
        binding.lv.setOnItemClickListener { p0, p1, p2, p3 ->
            startActivity(DetailActivity::class.java){
                putExtra("id",viewModel.newsData.value?.get(p2)?.id)
            }
        }
    }

    fun getNews(id: Int?){
        retrofit {
            getHomeNews(id,null).run {
                onSuccess{
                    viewModel.newsData.postValue(it.rows)
                }
            }
        }
    }
}